package com.purchase.jd.login;

import cn.hutool.http.HttpResponse;
import com.purchase.jd.start.Spike;
import com.purchase.jd.commodity.JdConstants;
import com.purchase.jd.http.HttpHeaders;
import com.purchase.jd.http.JdHttpUtil;
import com.purchase.util.HttpUtils;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;

import java.io.IOException;
import java.net.HttpCookie;
import java.util.List;

/**
 * @author  saw
 * @date 2021/11/8
 */
public class Login {

    public static List<HttpCookie> Cookies;

    public static String ticket = "";

    /**
     * 京东登录方法
     * @throws IOException
     * @throws InterruptedException
     */
    public static void jdLogin() throws IOException, InterruptedException {
        //获取二维码
        long now = System.currentTimeMillis();
        HttpResponse response = JdHttpUtil.scanCode(HttpHeaders.getHeaders(), JdConstants.SCAN_URL + now);
        //打开二维码
        Runtime.getRuntime().exec("cmd /c QCode.png");
        System.out.println("返回cookie:"+ response.getCookieStr());
        String token = response.getCookieValue("wlfstk_smdl");
        Cookies = response.getCookies();
        //判断是否扫二维码
        while (true) {
            String checkUrl = "https://qr.m.jd.com/check?appid=133&callback=jQuery"
                    + (int) ((Math.random() * (9999999 - 1000000 + 1)) + 1000000)
                    + "&token=" + token
                    + "&_=" + System.currentTimeMillis();
            HttpResponse qrCodeResult = HttpUtils.get(HttpHeaders.getHeaders(), checkUrl, Cookies);
            String qrCode = qrCodeResult.body();
            if (qrCode.contains("二维码未扫描")) {
                System.out.println("二维码未扫描，请扫描二维码登录");
            } else if (qrCode.contains("请手机客户端确认登录")) {
                System.out.println("请手机客户端确认登录");
            } else {
                ticket = qrCode.split("\"ticket\" : \"")[1].split("\"\n" +
                        "}\\)")[0];
                System.out.println("已完成二维码扫描登录");
                close();
                break;
            }
            Thread.sleep(3000);
        }
        //验证，获取cookie
        HttpResponse ticketResponse = HttpUtils
                .get(HttpHeaders.getHeaders(), JdConstants.CHECK_TICKET_URL + ticket, Cookies);
        System.out.println(ticketResponse.body());
        Cookies = ticketResponse.getCookies();
    }

    public static void close() {
//        通过窗口标题获取窗口句柄
        final User32 user32 = User32.INSTANCE;
        user32.EnumWindows(new WinUser.WNDENUMPROC() {
            @Override
            public boolean callback(WinDef.HWND hWnd, Pointer arg1) {
                char[] windowText = new char[512];
                user32.GetWindowText(hWnd, windowText, 512);
                String wText = Native.toString(windowText);
                if (wText.isEmpty()) {
                    return true;
                }
                if (wText.contains("照片")) {
                    hWnd = User32.INSTANCE.FindWindow(null, wText);
                    User32.INSTANCE.SendMessage(hWnd, 0X10, null, null);
                }
                return true;
            }
        }, null);
    }


}
